package net.guruqu.rvoView;

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridLayout;
import java.awt.event.KeyEvent;
import java.util.HashMap;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JToggleButton;

import com.gibbon.jme.context.JmeContext;

public class MapDefComponent extends JPanel {

	static enum ActionHint {
		addAgent, addControlPoint, addGoal, addRoadLine, select
	}

	private static final long serialVersionUID = 1L;

	private JToggleButton addAgent = null;

	private JToggleButton addAgent1 = null;;

	private JToggleButton addControlPoint = null;
	private JButton loadMap = null;
	private JToggleButton addRoadLine = null;
	private JButton clearButton = null;
	private JPanel controlPanel = null;
	MapDefView defView;
	GroundDefComponent gdp;
	JFrame gdpFrame;
	private JLabel jLabel = null;
	ModelRepository modelRepository;
	private JButton saveButton = null;

	private JToggleButton selectObject = null;

	private JPanel viewPanel = null;

	private JSlider zoomSlide = null;

	/**
	 * This is the default constructor
	 */
	public MapDefComponent(ModelRepository modelRepository) {
		super();
		HashMap<String, String> prop = new HashMap<String, String>();
		// prop.put(MapDefView.MD_ENV_MODEL, "model.txt");
		prop.put(MapDefView.MD_ROADMAP_FILE, ConfigFile.get().getProperty("envDef"));
		defView = new MapDefView(prop, modelRepository);
		defView.setHintMouseMode(ActionHint.addRoadLine);
		this.modelRepository = modelRepository;
		// try {
		// defView.loadRoadMap();
		// } catch (Exception e) {
		// e.printStackTrace();
		// }
		initialize();

		// Attach the canvas to the Map Define View as soon as the
		// context become available, so that the renderer will not be
		// null, and RenderPass class can function correctly
		new Thread() {
			@Override
			public void run() {
				try {
					canvasContext.waitFor();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				defView.setCanvas(canvasContext);
			}
		}.start();
	}

	/**
	 * This method initializes addAgent1
	 * 
	 * @return javax.swing.JToggleButton
	 */
	private JToggleButton getAddAgent1() {
		if (addAgent1 == null) {
			addAgent1 = new JToggleButton();
			addAgent1.setText("Add Agent");
			addAgent1.addItemListener(new java.awt.event.ItemListener() {
				public void itemStateChanged(java.awt.event.ItemEvent e) {
					defView.setHintMouseMode(ActionHint.addAgent);
					repaint();
				}
			});
		}
		return addAgent1;
	}

	/**
	 * This method initializes addControlPoint
	 * 
	 * @return javax.swing.JToggleButton
	 */
	private JToggleButton getAddControlPoint() {
		if (addControlPoint == null) {
			addControlPoint = new JToggleButton();
			addControlPoint.setText("Add Control Point");
			addControlPoint.addItemListener(new java.awt.event.ItemListener() {
				public void itemStateChanged(java.awt.event.ItemEvent e) {
					defView.setHintMouseMode(ActionHint.addControlPoint);
					canvasContext.getCanvas().repaint();
				}
			});
		}
		return addControlPoint;
	}

	/**
	 * This method initializes loadMap
	 * 
	 * @return javax.swing.JToggleButton
	 */
	private JButton getLoadMap() {
		if (loadMap == null) {
			loadMap = new JButton();
			loadMap.setText("Load RoadMap");
			loadMap.setMnemonic(KeyEvent.VK_UNDEFINED);
			loadMap.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					
					try {
						defView.loadRoadMap();
						JOptionPane.showMessageDialog(null,
						"Project load successful");
					} catch (Exception ex) {
//						ex.printStackTrace();
						JOptionPane.showMessageDialog(null, ex.getMessage());
					}
					canvasContext.getCanvas().repaint();
				}
			});
			
		}
		return loadMap;
	}

	/**
	 * This method initializes addRoadLine
	 * 
	 * @return javax.swing.JToggleButton
	 */
	private JToggleButton getAddRoadLine() {
		if (addRoadLine == null) {
			addRoadLine = new JToggleButton();
			addRoadLine.setText("Add RoadLine");
			addRoadLine.setSelected(true);
			addRoadLine.addItemListener(new java.awt.event.ItemListener() {
				public void itemStateChanged(java.awt.event.ItemEvent e) {
					defView.setHintMouseMode(ActionHint.addRoadLine);
					canvasContext.getCanvas().repaint();
				}
			});
		}
		return addRoadLine;
	}

	/**
	 * This method initializes clearButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getClearButton() {
		if (clearButton == null) {
			clearButton = new JButton();
			clearButton.setText("Clear Scene");
			clearButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					defView.clear();
					canvasContext.getCanvas().repaint();
				}
			});
		}
		return clearButton;
	}

	/**
	 * This method initializes controlPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getControlPanel() {
		if (controlPanel == null) {

			jLabel = new JLabel();
			jLabel.setText("");
			GridLayout gridLayout = new GridLayout();
			gridLayout.setRows(5);
			gridLayout.setColumns(2);
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.gridx = -1;
			gridBagConstraints1.gridy = -1;

			controlPanel = new JPanel();
			controlPanel.setLayout(gridLayout);
			controlPanel.add(getZoomSlide(), null);
			controlPanel.add(jLabel, null);

			controlPanel.add(getLoadMap(), null);
			controlPanel.add(getSelectObject(), null);
			controlPanel.add(getSaveButton(), null);
			controlPanel.add(getAddRoadLine(), null);
			controlPanel.add(getAddControlPoint(), null);
			controlPanel.add(getAddAgent1(), null);
			controlPanel.add(getClearButton(), null);
			ButtonGroup buttonGroup = new ButtonGroup();
			buttonGroup.add(getAddRoadLine());
			buttonGroup.add(getAddControlPoint());
			buttonGroup.add(getSelectObject());
			buttonGroup.add(getAddAgent1());
		}
		return controlPanel;
	}

	/**
	 * This method initializes saveButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getSaveButton() {
		if (saveButton == null) {
			saveButton = new JButton();
			saveButton.setText("Save RoadMap");
			saveButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					try {
						defView.saveRoadMap();
						JOptionPane.showMessageDialog(null,
								"Saved successfully to project");
					} catch (Exception e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
						JOptionPane.showMessageDialog(null, e1.getMessage(),
								"Error", JOptionPane.ERROR_MESSAGE);
					}

				}
			});
		}
		return saveButton;
	}

	/**
	 * This method initializes selectObject
	 * 
	 * @return javax.swing.JToggleButton
	 */
	private JToggleButton getSelectObject() {
		if (selectObject == null) {
			selectObject = new JToggleButton();
			selectObject.setText("Select Object");
			selectObject.addItemListener(new java.awt.event.ItemListener() {
				public void itemStateChanged(java.awt.event.ItemEvent e) {
					defView.setHintMouseMode(ActionHint.select);
					canvasContext.getCanvas().repaint();
				}
			});
		}
		return selectObject;
	}

	JmeContext canvasContext;  //  @jve:decl-index=0:

	/**
	 * This method initializes viewPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getViewPanel() {
		if (viewPanel == null) {
			viewPanel = new JPanel();
			viewPanel.setLayout(new BorderLayout());

			// Canvas canvas = defView.getCanvas();
			JmeContext canvas = ModelUtil.createCanvas(640, 480,
					modelRepository.getModelContext());
			this.canvasContext = canvas;
			viewPanel.add(canvas.getCanvas());
		}
		return viewPanel;
	}

	/**
	 * This method initializes zoomSlide
	 * 
	 * @return javax.swing.JSlider
	 */
	private JSlider getZoomSlide() {
		if (zoomSlide == null) {
			zoomSlide = new JSlider();
			zoomSlide.setName("zoomSlide");

			float v1 = (float) Math.log(0.1);
			float v2 = (float) Math.log(100);
			zoomSlide.setValue(-(int) (v1 * (100f / (v2 - v1))));
			zoomSlide.addChangeListener(new javax.swing.event.ChangeListener() {
				public void stateChanged(javax.swing.event.ChangeEvent e) {
					// System.out.println(zoomSlide.getValue());
					float v1 = (float) Math.log(0.1);
					float v2 = (float) Math.log(100);

					float v = zoomSlide.getValue() / 100.0f * (v2 - v1) + v1;
					float nz = (float) Math.exp(v);
					defView.setZoom(nz);

					jLabel.setText("zoom:" + ((int) (100 * nz)) / 100.0f);
					canvasContext.getCanvas().repaint();
				}
			});
		}
		return zoomSlide;
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(300, 200);
		this.setLayout(new BorderLayout());
		this.add(getControlPanel(), BorderLayout.SOUTH);
		this.add(getViewPanel(), BorderLayout.CENTER);

	}

}
